package controllers;

import java.util.ResourceBundle;

import helpers.EncryptionClass;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import beans.RepositoryBeanRemote;
import beans.UserBeanRemote;

import dto.DTOUser;

@ManagedBean
@RequestScoped
public class NewUserController {

	@EJB
	UserBeanRemote ubr;
	
	@EJB
	RepositoryBeanRemote rbr;

	@ManagedProperty(value = "#{newUser}")
	private DTOUser newUser;

	@ManagedProperty(value = "#{language}")
	private LocaleController language;

	private String renderCreateUser;
	private Integer selectedRep;

	/**
	 * @return the newUser
	 */
	public DTOUser getNewUser() {
		return newUser;
	}

	/**
	 * @param newUser
	 *            the newUser to set
	 */
	public void setNewUser(DTOUser newUser) {
		this.newUser = newUser;
	}

	public String createOkRedirect() {
		renderCreateUser = null;
		return "ViewUsers.xhtml?faces-redirect=true";
	}

	/**
	 * 
	 * Getter for locale.
	 * 
	 */
	public LocaleController getLanguage() {
		return language;
	}

	/**
	 * 
	 * Setter for locale.
	 * 
	 */
	public void setLanguage(LocaleController language) {
		this.language = language;
	}

	/**
	 * @return the selectedRep
	 */
	public Integer getSelectedRep() {
		return selectedRep;
	}

	/**
	 * @param selectedRep the selectedRep to set
	 */
	public void setSelectedRep(Integer selectedRep) {
		this.selectedRep = selectedRep;
	}

	public String adauga() {

		/**
		 * @author soreas
		 * 
		 *         Password encryption:
		 */
		EncryptionClass encryptor = new EncryptionClass();
		String pass = encryptor.encryptPassword(newUser.getName());
		// internationalization
		HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
				.getExternalContext().getSession(true);
		LocaleController language = (LocaleController) session
				.getAttribute("language");
		String messageBundleName = "errorMessages.errors";
		if (language.getLanguage() == "ro") {
			messageBundleName += "_ro";
		} else if (language.getLanguage() == "hu") {
			messageBundleName += "_hu";
		} else if (language.getLanguage() == "de") {
			messageBundleName += "_de";
		}
		ResourceBundle messageBundle = ResourceBundle
				.getBundle(messageBundleName);
		// ////////
		try {

			newUser.setPassword(pass);
			newUser.setType("user");
			ubr.addUser(newUser, language.getLanguage());
			Integer idUser =ubr.getUserIdByName(newUser.getName());
			rbr.addUserToRepository(idUser, selectedRep);
			setRenderCreateUser(messageBundle.getString("userCreated"));
		} catch (Exception e) {
			setRenderCreateUser(messageBundle.getString("userNotCreated"));
		}

		return "ok";
	}

	/**
	 * @return the renderCreateUser
	 */
	public String getRenderCreateUser() {
		return renderCreateUser;
	}

	/**
	 * @param renderCreateUser
	 *            the renderCreateUser to set
	 */
	public void setRenderCreateUser(String renderCreateUser) {
		this.renderCreateUser = renderCreateUser;
	}
}
